package com.fallsvc.ai.controller;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.util.List;

/**
 * @auther falls_vc
 * description:
 * @date 2025/11/4  22:22
 */
@RequestMapping("/chat")
@RestController
public class ChatClientController {
    @Autowired
    private ChatClient chatClient;



    @GetMapping("/ai")
    public String generation(String userInput) {
        return this.chatClient.prompt()
                // 用户输入
                .user(userInput)
                // 发送请求
                .call()
                // 返回String
                .content();
    }

    @GetMapping(value = "/stream",produces = "text/html;charset=utf-8")
    public Flux<String> string(String userInput) {
        return this.chatClient.prompt()
                // 用户输入
                .user(userInput)
                // 发送请求
                .stream()
                // 返回String
                .content();
    }


    record Recipe(String dish, List<String> ingredients) {} // 定义实体类

    @RequestMapping("/entity")
    public String entity(String userInput) {
        Recipe recipe = this.chatClient.prompt()
                .user(String.format("帮我生成%s的食谱",userInput))
                .call()
                .entity(Recipe.class);
        return recipe.toString();
    }

}
